<!-- 项目审批 -->
<template>
	<view style="margin-bottom: 100rpx">
		<cu-custom bgColor="bg-blue2" :isBack="true" @back="backedPage"><block slot="backText">返回</block><block slot="content">库存编辑</block></cu-custom>
		<form @submit="onSubmit">
			<view class="cu-form-group">
				<view class="title">卷号</view>
				<input v-model="item.innerBatch" disabled="disabled" placeholder="卷号" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">货品名称</view>
				<input v-model="item.goodsName" disabled="disabled" placeholder="货品名称" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">规格</view>
				<input v-model="item.spec" disabled="disabled" placeholder="规格" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">产地</view>
				<input v-model="item.maker" disabled="disabled" placeholder="产地" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">型号</view>
				<input v-model="item.model" disabled="disabled" placeholder="型号" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">材质</view>
				<input v-model="item.materialCode" disabled="disabled" placeholder="材质" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">货位</view>
				<input v-model="item.goodsPosi" placeholder="选择货位" name="input" @blur="checkPosi"></input>
				<view class="cuIcon-form text-blue" @tap="selectPosi()">选择货位</view>
			</view>
			<view class="cu-form-group">
				<view class="title">堆号</view>
				<input v-model="item.heapNum" placeholder="堆号" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">库存数量</view>
				<input v-model="item.qty" disabled="disabled" placeholder="输入数量" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">库存重量</view>
				<input v-model="item.weight" disabled="disabled" placeholder="输入重量" name="input"></input>
			</view>
<!-- 			<view class="cu-form-group">
				<view class="title">入库时间</view>
				<input v-model="item.goodsInDat" placeholder="输入入库时间" name="input"></input>
			</view> -->
			<view>
				<button form-type="submit" class="cu-btn block bg-blue margin-tb-sm lg" :disabled="loading" type="">
					<text :class="loading ? 'cuIcon-loading2 cuIconfont-spin' : ''"></text>保存</button>
			</view>
		</form>
		<view class="cu-modal" :class="isShowGoodsPosiDialog ? 'show' : ''">
			<view class="cu-dialog" :style="{ marginTop: customBarH+'px' }">
				<view class="cu-bar bg-white justify-end">
					<view class="content">选择货位</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view style="height: 350px; overflow: auto;">
					<select-goods-posi style="height: 340px;" @selected="selectedPosi"/>
				</view>
				<view class="cu-bar bg-white justify-end">
					<view class="action">
						<button class="cu-btn line-green text-green" @tap="hideModal">取消</button>
						<button class="cu-btn bg-green margin-left" @tap="hideModal">确定</button>
		
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniTag from '@/components/uni-tag/uni-tag.vue'
	import uniIcons from '@/components/uni-icons/uni-icons.vue'
	import uniCombox from '@/components/uni-combox/uni-combox.vue'
	import selectGoodsPosi from './selectGoodsPosi.vue'
	import {
		mapGetters
	} from 'vuex'
	import {
		getRandomArrayElements,
		filePreview,
		formatStatus
	} from '@/utils/index.js'
	import { formatDate } from '@/utils/datetime.js'
	export default {
		components: {
			uniTag,
			uniIcons,
			uniCombox,
			selectGoodsPosi
		},
		computed: mapGetters(['user']),
		data() {
			return {
				item: { innerBatch: '', goods: {name:''}, specOptions:[], modelOptions:[], materialCodeOptions:[], makerOptions:[] },
				id:0,
				weightMethodOptions:['抄码','过地磅','过电子磅','理算','平均件重'],
				workGroupOptions:[],
				machineOptions:[],
				goodsOptions: [],
				data: [],
				defaultProps: {
					children: 'children',
					label: 'fullName'
				},
				isShowGoodsPosiDialog: false,
				selectedPosiObj: {},
				loading: false,
				goods: null,
				customBarH:this.CustomBar+20,
			}
		},
		onLoad(options) {

			this.id = options.id
			if (this.id > 0) {
				this.$minApi.inStockGoodsDetail(this.id).then(res => {
					const data = res
					if (data) {
						this.item = data
						if (data.inStockGoodsStacks && data.inStockGoodsStacks.length>0){
							this.item.innerBatch = data.inStockGoodsStacks[0].innerBatch
						}
		
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {
				})
			}
						
			this.$minApi.getWorkGroups().then(res => {
				const data = res
				if (data) {
					this.workGroupOptions =	data.map(a => a.name)
					if(this.workGroupOptions && this.workGroupOptions.length>0){
						this.item.workGroup=this.workGroupOptions[0]
					}
				}
			}).catch(err => {
				console.log(err)
			}).finally(() => {
				this.isLoading = false
			})
			
			this.$minApi.getMachines().then(res => {
				const data = res
				if (data) {
					this.machineOptions = data.map(a => a.name)
				}
			}).catch(err => {
				console.log(err)
			}).finally(() => {
				this.isLoading = false
			})
			
			const params = {
				filter: '',
				sorting: '',
				pageIndex: '10000',
				pageSize: 10
			  }
			this.$minApi.getGoods(params).then(res => {
				const data = res.list
				if (data) {
					this.goods = data
					this.goodsOptions = data.map(a => a.name)
				}
			}).catch(err => {
				console.log(err)
			}).finally(() => {
				this.isLoading = false
			})

		},
		methods: {
			filePreview,
			formatStatus,
			formatDate,
			onSubmit(e) {  
				
				this.loading = true
				const _this = this

				if (this.id > 0) {
					// this.item.qty = Number(this.item.qty)
					// this.item.weight = Number(this.item.weight)
					// this.item.goodsInBillId =  Number(this.goodsInBillId)
					
					_this.$minApi.updateInStockGoodsDetail(this.item.id, this.item).then(res => {
						uni.showToast({
						    title: '修改成功',
						    duration: 2000
						})
						
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/inStockGoodsDetailChange/list`
							})
						}, 2000)
				
					}).catch(err => {
						uni.showToast({
							icon: 'none',
						    title: '修改失败',
						    duration: 2000
						})
					}).finally(() => {
						this.loading = false
					})
				}
				
			},
			hideModal(e) {
				if (this.selectedPosiObj && this.selectedPosiObj.data) {
					this.item.goodsPosi = this.selectedPosiObj.data.fullName
				}
				this.isShowGoodsPosiDialog = false
			},
			selectPosi() {	
				this.isShowGoodsPosiDialog = true
			},
			selectedPosi(val) {
				this.selectedPosiObj = val
			},
			selectedGoods(val) {
				if (this.goods && val) {
					const goods = this.goods.find(f => f.name === val)
					
					if (goods) {
						
						this.item.goodsId = goods.id
						this.item.qtyUnit = goods.qtyUnit
						this.item.weightUnit = goods.weightUnit
						
						this.item.spec = ''
						this.item.model = ''
						this.item.materialCode = ''
						this.item.maker = ''
						
						this.bindSpec(this.item, goods.id)
						this.bindModel(this.item, goods.id)
						this.bindMaterialCode(this.item, goods.id)
						this.bindMaker(this.item, goods.id)
					}
				}
				
			},
			checkPosi() {
				
				const data = uni.getStorageSync('storage_key_getGoodsWareHouses')
				if(data && data.includes(f=>f.fullName === this.item.goodsPosi)){
					
				}else{
					uni.showToast({
						icon: 'none',
					    title: '货位不存在',
					    duration: 2000
					})
				}
			},
			bindSpec(item, goodsID) {
			  this.$minApi.getGoodsPropertys({
				filter: '规格',
				goodsID: goodsID,
				pageIndex: 10000,
				pageSize: 10
			  }).then((res) => {
				const options = res.list.map(a => a.propertyValue)
				item.specOptions = options
			  }).catch(err => {
				console.log(err)
			  }).finally(() => { })
			},
			bindModel(item, goodsID) {
			  this.$minApi.getGoodsPropertys({
				filter: '型号',
				goodsID: goodsID,
				pageIndex: 10000,
				pageSize: 10
			  }).then((res) => {
				const options = res.list.map(a => a.propertyValue)
				item.modelOptions = options
			  }).catch(err => {
				console.log(err)
			  }).finally(() => { })
			},
			bindMaterialCode(item, goodsID) {
			  this.$minApi.getGoodsPropertys({
				filter: '材质',
				goodsID: goodsID,
				pageIndex: 10000,
				pageSize: 10
			  }).then((res) => {
				const options = res.list.map(a => a.propertyValue)
				item.materialCodeOptions = options
			  }).catch(err => {
				console.log(err)
			  }).finally(() => { })
			},
			bindMaker(item, goodsID) {
			  this.$minApi.getGoodsPropertys({
				filter: '产地',
				goodsID: goodsID,
				pageIndex: 10000,
				pageSize: 10
			  }).then((res) => {
				const options = res.list.map(a => a.propertyValue)
				item.makerOptions = options
			  }).catch(err => {
				console.log(err)
			  }).finally(() => { })
			},
			backedPage() {
				uni.redirectTo({
					url: `/pages/inStockGoodsDetailChange/list`
				})
			}
		}
	}
</script>

<style>

</style>
